package com.example.music.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.music.R;
import com.example.music.entity.DataMusic;

import java.util.List;

public class UpListAdapter extends BaseAdapter {

    private List<DataMusic> musics;

    public UpListAdapter(List<DataMusic> musics) {
        this.musics = musics;
    }

    @Override
    public int getCount() {
        return musics.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        if(view==null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.ll = view.findViewById(R.id.ll_back);
            viewHolder.up = view.findViewById(R.id.serial_number);
            viewHolder.author = view.findViewById(R.id.tv_author);
            viewHolder.name = view.findViewById(R.id.tv_name);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.ll.setBackgroundColor(position%2==0? Color.parseColor("#ECECEC") : Color.parseColor("#DCDCDC"));
        viewHolder.up.setText(position+1+"");
        if(position<3){
            viewHolder.up.setTextColor(Color.RED);
        }
        viewHolder.name.setText(musics.get(position).getName());
        viewHolder.author.setText(musics.get(position).getAuthor());
        return view;
    }
    class ViewHolder{
        LinearLayout ll;
        TextView up;
        TextView name;
        TextView author;
    }
}
